package com.dxf.kafka.consumer;

import com.dxf.kafka.domain.KafkaMysql;
import com.dxf.kafka.domain.SysUser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.apache.kafka.common.serialization.Deserializer;

import java.io.IOException;
import java.util.Map;

public class UserDeserializer implements Deserializer<KafkaMysql> {

    private ObjectMapper objectMapper;

    @Override
    public void configure(Map<String, ?> configs, boolean isKey) {
        objectMapper=new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
    }

    @Override
    public void close() {
       this.objectMapper=null;
    }

    @Override
    public KafkaMysql deserialize(String s, byte[] bytes) {
        try {
            return objectMapper.readValue(bytes,KafkaMysql.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
